<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="文章名称">
      <el-input v-model="form.title"></el-input>
    </el-form-item>
    <el-form-item label="文章内容">
      <rich-text :value.sync="form.content" height="500" />
    </el-form-item>
    <el-form-item label="类型">
      <el-select v-model="form.type" placeholder="请选择">
        <el-option label="原创" :value="1"></el-option>
        <el-option label="转载" :value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="转载地点">
      <el-input v-model="form.reprintName"></el-input>
    </el-form-item>
    <el-form-item label="转载地址">
      <el-input v-model="form.reprintUrl"></el-input>
    </el-form-item>
    <el-form-item label="标签">
      <enter-label :value.sync="form.tipsArr" />
    </el-form-item>
    <el-form-item label="是否激活">
      <el-switch v-model="form.bState" :active-value="1" :inactive-value="0">
      </el-switch>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="target('submit')">立即创建</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// import richText from "@/components/rich_text";
import enterLabel from "@/components/enter_label";

export default {
  layout: "admin",
  components: {
    // richText,
    enterLabel,
  },
  mounted() {
    if (this.$route.params.id != "data") {
      this.target("init");
    }
  },
  watch: {
    "form.tipsArr"() {
      this.form.tips = this.form.tipsArr.join("|");
      console.log(this.form.tips);
    },
  },
  data() {
    return {
      form: {
        title: "",
        content: "",
        type: 1,
        bState: 1,
        tipsArr: [],
        tips: "",
      },
    };
  },
  methods: {
    target(key) {
      switch (key) {
        case "init":
          this.getData();
          break;
        case "add":
          this.$router.push({
            path: "/admin/banner/add/data",
          });
          break;
        case "submit":
          this.submit();
          break;
        default:
          break;
      }
    },
    async getData() {
      let data = await this.$axios.post(
        `admin/note/find/${this.$route.params.id}`
      );
      //  || [];
      if (data.data.tips) {
        data.data.tipsArr = data.data.tips.split("|");
      } else {
        data.data.tipsArr = [];
      }
      console.log(data.data);
      this.form = data.data;
    },
    async submit() {
      let pushData = JSON.parse(JSON.stringify(this.form));
      delete pushData.tipsArr;
      let data = await this.$axios.post("/admin/note/update", pushData);
      this.$message.success("更新成功");
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>